﻿using Microsoft.AspNetCore.Mvc;
using System.Data;
using ZhiJiao.Models.Entities.Enums;
using ZhiJiao.Service.Abstracts;
using ZhiJiao.Service.Dto;

namespace ZHIJIAO.Controllers
{
    public class RuleController : Controller
    {
        private readonly BackJson _res;
        private readonly ICurrentUser _currentUser;
        private readonly IRuleService _ruleService;

        public RuleController(BackJson res,
                              ICurrentUser currentUser,
                              IRuleService ruleService)
        { 
            _res = res;
            _currentUser = currentUser;
            _ruleService = ruleService;
        }


        public IActionResult Index()
        {
            ViewBag.UserType = Convert.ToInt32(_currentUser.User.Type);
            if (_currentUser.User.Type == UserType.Instruction)
            {
                ViewBag.InsId = _currentUser.User.Instruction!.Id;
            }
            else
            {
                ViewBag.InsId = 0;
            }
            return View();
        }


        /// <summary>
        ///  获取规则列表
        /// </summary>
        [HttpGet("api/rule/get-rule-list")]
        public async Task<IActionResult> GetRuleList(long insId, string? name, RuleType? type)
        {
            return Json(await _ruleService.GetRuleList(insId,name,type));
        }

        /// <summary>
        /// 保存参与规则
        /// </summary>
        [HttpPost("app/rule/save")]
        public async Task<IActionResult> Save([FromBody]RuleDto dto)
        {
            return Json(await _ruleService.Save(dto));
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        [HttpGet("api/rule/delete-rule")]
        public async Task<IActionResult> Delete(long id)
        {
            return Json(await _ruleService.DeleteRule(id));
        }

        /// <summary>
        /// 获取单个参与规则
        /// </summary>
        [HttpGet("api/rule/get-sigle-rule")]
        public async Task<IActionResult> GetSingleRule(long id)
        {
            return Json(await _ruleService.GetSingleRule(id));
        }

        [HttpGet("api/rule/switch-isActive")]
        public async Task<IActionResult> IsActiveSwitch(long id, string value)
        {
            return Json(await _ruleService.IsActiveSwitch(id,value));
        }

        [HttpGet("api/rule/get-evaluation-rules")]
        public async Task<IActionResult> GetEvaluationRule(long id)
        {
            return Json(await _ruleService.GetEvaluationRules(id));
        }
    }
}
